/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.manager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;

/**
 * CSV file output to local system Might need to use RandomAccessFile
 * implementation to allow writing and reading to this file at the same time.
 * 
 * @author Kyle O'Connor
 * 
 */
public class FileOutputManager extends Manager {

	private final static Logger log = Logger.getLogger(FileOutputManager.class);

	public static final String OUTPUT_FILENAME = "deployment_data.csv";

	private String fileName;
	private BufferedWriter writer;

	public FileOutputManager(MainModel model, Properties props) throws IOException {
		super(model, props);

		this.fileName = OUTPUT_FILENAME;
		File outputFile = new File(fileName);
		if (outputFile.exists()) {
			outputFile.delete();
			log.debug("Deleted existing data file");
		}
		boolean created = outputFile.createNewFile();
		if (created) {
			log.debug("Created new output file");
		} else {
			throw new IOException("Error overwriting file");
		}
		writer = new BufferedWriter(new FileWriter(outputFile));
		setup();
	}

	public void setup() throws IOException {
		writer.write("Node");
		writer.write(',');
		writer.write("Date");
		writer.write(',');
		writer.write("Small Count");
		writer.write(',');
		writer.write("Large Count");
		writer.write(',');
		writer.write("Humidity");
		writer.write(',');
		writer.write("Temperature");
		writer.write(',');
		writer.write("Conditions");
		writer.write(',');
		writer.write("Small Concentration");
		writer.write(',');
		writer.write("Large Concentration");
		writer.newLine();
		writer.flush();
	}

	public void appendData(String data) throws IOException {
		writer.append(data);
		writer.newLine();
		writer.flush();
	}

	public void appendData(String nodeID, String date, String smallCount, String largeCount) throws IOException {
		writer.append(nodeID);
		writer.append(',');
		writer.append(date);
		writer.append(',');
		writer.append(smallCount);
		writer.append(',');
		writer.append(largeCount);
		writer.newLine();
		writer.flush();
	}

	public void appendData(String nodeID, String date, int smallCount, int largeCount, double humidity, double temp,
			String conditions, double smallConc, double largeConc) throws IOException {
		writer.append(nodeID);
		writer.append(',');
		writer.append(date);
		writer.append(',');
		writer.append(String.valueOf(smallCount));
		writer.append(',');
		writer.append(String.valueOf(largeCount));
		writer.append(',');
		writer.append(String.valueOf(humidity));
		writer.append(',');
		writer.append(String.valueOf(temp));
		writer.append(',');
		writer.append(conditions);
		writer.append(',');
		writer.append(String.valueOf(smallConc));
		writer.append(',');
		writer.append(String.valueOf(largeConc));
		writer.newLine();
		writer.flush();
	}

	private void close() throws IOException {
		writer.flush();
		writer.close();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.drexel.sd0910.ece01.aqmon.manager.Manager#shutdown()
	 */
	@Override
	public void shutdown() throws Exception {
		this.close();
	}

}
